<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace longbingcore\wxcore;

require_once EXTEND_PATH . "baiying/BYTokenClient.php";
require_once EXTEND_PATH . "baiying/BYGetTokenClient.php";
class BaiYing
{
    protected $uniacid;
    protected $access_token;
    protected $company_id;
    protected $config;
    public function __construct($uniacid)
    {
        $this->uniacid = $uniacid;
        $key = "by_access_token" . $uniacid;
        $config = \app\baiying\model\BaiYingConfig::getInfo(["uniacid" => $uniacid]);
        $access_token = getCache($key, $uniacid);
        if(empty($access_token)) {
            $con = new \BYGetTokenClient($config["app_key"], $config["app_secret"]);
            $data = $con->get_token("platform", $config["company_id"]);
            if(isset($data["code"]) && $data["code"] == 200 && isset($data["data"]["access_token"])) {
                $access_token = $data["data"]["access_token"];
                setCache($key, $access_token, 604800);
            }
        }
        $this->company_id = $config["company_id"];
        $this->access_token = $access_token;
        $this->config = $config;
    }
    public function scriptList()
    {
        if(empty($this->access_token)) {
            return ["code" => 400, "resultMsg" => "请配置基础信息"];
        }
        $client = new \BYTokenClient($this->access_token);
        $method = "byai.openapi.robot.list";
        $api_version = "1.0.0";
        $my_params = ["companyId" => $this->company_id, "robotStatus" => 1];
        $my_files = [];
        $data = $client->post($method, $api_version, $my_params, $my_files);
        if($data["code"] == 200) {
            return $data["data"];
        }
        return $data;
    }
    public function phoneList()
    {
        if(empty($this->access_token)) {
            return ["code" => 400, "resultMsg" => "请配置基础信息"];
        }
        $client = new \BYTokenClient($this->access_token);
        $method = "byai.openapi.phone.list";
        $api_version = "1.0.0";
        $my_params = ["companyId" => $this->company_id];
        $my_files = [];
        $data = $client->post($method, $api_version, $my_params, $my_files);
        if($data["code"] == 200) {
            return $data["data"];
        }
        return $data;
    }
    public function make($type = 1)
    {
        if(empty($this->access_token)) {
            return ["code" => 400, "resultMsg" => "请配置基础信息"];
        }
        $time = date("H:i:s");
        if(!("09:00:00" <= $time && $time <= "20:00:00")) {
            return ["code" => 400, "resultMsg" => "不在规定时间内"];
        }
        if($type == 1 && (empty($this->config["wait_pay_script_id"]) || empty($this->config["wait_pay_phone_id"])) || $type == 2 && (empty($this->config["order_radar_script_id"]) || empty($this->config["order_radar_phone_id"]))) {
            return ["code" => 400, "resultMsg" => "话术ID或外呼号码未配置"];
        }
        $client = new \BYTokenClient($this->access_token);
        $method = "byai.openapi.calljob.create";
        $api_version = "1.0.0";
        $my_params = ["callJobName" => "系统自动创建" . date("YmdHis"), "callJobType" => "2", "companyId" => $this->company_id, "robotDefId" => $type == 1 ? $this->config["wait_pay_script_id"] : $this->config["order_radar_script_id"], "userPhoneIds" => "[" . ($type == 1 ? $this->config["wait_pay_phone_id"] : $this->config["order_radar_phone_id"]) . "]", "concurrencyQuota" => 50];
        $my_files = [];
        $data = $client->post($method, $api_version, $my_params, $my_files);
        return $data;
    }
    public function importPhone($job_id, $phoneList)
    {
        if(empty($phoneList) || empty($this->access_token)) {
            return ["code" => 400, "resultMsg" => "请配置基础信息"];
        }
        $customerInfoVOList = [];
        foreach ($phoneList as $key => $item) {
            $customerInfoVOList[] = ["name" => (string) $key, "phone" => (string) $item];
        }
        $client = new \BYTokenClient($this->access_token);
        $method = "byai.openapi.calljob.customer.import";
        $api_version = "1.0.0";
        $my_params = ["callJobId" => $job_id, "companyId" => $this->company_id, "customerInfoVOList" => json_encode($customerInfoVOList)];
        $my_files = [];
        $filename = "./log/baiying/baiying" . date("Y-m-d") . ".log";
        $str = [$method, $api_version, $my_params, $my_files];
        file_put_contents($filename, time() . "\n" . json_encode($str) . "\n" . "\n", FILE_APPEND | LOCK_EX);
        $data = $client->post($method, $api_version, $my_params, $my_files);
        file_put_contents($filename, time() . "\n" . json_encode($data) . "\n" . "\n", FILE_APPEND | LOCK_EX);
        return $data;
    }
    public function start($job_id)
    {
        if(empty($this->access_token)) {
            return ["code" => 400, "resultMsg" => "请配置基础信息"];
        }
        $client = new \BYTokenClient($this->access_token);
        $method = "byai.openapi.calljob.execute";
        $api_version = "1.0.0";
        $my_params = ["callJobId" => $job_id, "command" => "1", "companyId" => $this->company_id];
        $my_files = [];
        $data = $client->post($method, $api_version, $my_params, $my_files);
        return $data;
    }
    public function water($phone, $type = 1)
    {
        try {
            $job = $this->make($type);
            if($job["code"] != 200) {
                throw new \Exception("创建任务-" . $job["resultMsg"]);
            }
            $job_id = $job["data"]["callJobId"];
            $phoneList = array_column($phone, "mobile", "user_name");
            $res = $this->importPhone($job_id, $phoneList);
            if($res["code"] != 200) {
                throw new \Exception("导入手机号-" . $res["resultMsg"]);
            }
            $res = $this->start($job_id);
            if($res["code"] != 200) {
                throw new \Exception("启动任务-" . $res["resultMsg"]);
            }
        } catch (\Exception $exception) {
            return ["code" => 400, "resultMsg" => $exception->getMessage()];
        }
        $insert = [];
        foreach ($phone as $add) {
            $add["job_id"] = $job_id;
            $add["type"] = $type;
            $add["update_time"] = time();
            $add["create_time"] = $add["update_time"];
            $insert[] = $add;
        }
        if($insert) {
            \app\baiying\model\BaiYingPhoneRecord::insertAll($insert);
        }
        return true;
    }
}

?>